package org.delay;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Set;

/**
 * @Classname RedisDelayQueueTopicRegistrar
 * @Description
 * @Date 2022/9/14 10:40
 * @Created by wangchangjiu
 */
@Slf4j
public class RedisDelayQueueTopicRegistrar implements DelayQueueTopicRegistrar {

    private String TOPIC_REGISTRAR_KEY = "topic_registrar_key:%s";

    private StringRedisTemplate stringRedisTemplate;

    public RedisDelayQueueTopicRegistrar(StringRedisTemplate stringRedisTemplate, String registrarService){
        this.stringRedisTemplate = stringRedisTemplate;
        this.TOPIC_REGISTRAR_KEY = String.format(TOPIC_REGISTRAR_KEY, registrarService);
    }

    @Override
    public void registrar(String topic) {
        stringRedisTemplate.opsForSet().add(TOPIC_REGISTRAR_KEY, topic);
        log.info("redis 注册器注册Topic:{} 成功， redisKey:{}", topic, TOPIC_REGISTRAR_KEY);
    }

    @Override
    public Set<String> pullAll(){
        Set<String> members = stringRedisTemplate.opsForSet().members(TOPIC_REGISTRAR_KEY);
        return members;
    }

}
